package com.mazong.serversecbasic.mapper;

import com.mazong.serversecbasic.pojo.TbRole;
import com.mazong.serversecbasic.pojo.TbRolePermisson;
import com.mazong.serversecbasic.pojo.TbUser;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;

@Mapper
@Component(value = "iDbSecurityMapper")
public interface IDbSecurityMapper {

    // 根据用户ID获取角色
    //@Select( "SELECT A.id,A.name FROM role A LEFT JOIN user_role B ON A.id=B.role_id WHERE B.user_id=${userId}" )
    @Select("SELECT id,name FROM role WHERE id IN (SELECT role_id FROM user_role WHERE user_id=${userId})")
    List<TbRole> getRolesByUserId(@Param("userId") Integer userId);

    // 根据用户名字获取用户信息
    @Select( "SELECT id,username,password FROM user WHERE username = #{username}" )
    TbUser loadUserByUsername(@Param("username") String username);

//    SELECT B.name AS roleName,C.url FROM role_permission AS A
//    LEFT JOIN role AS B ON A.role_id=B.id
//    LEFT JOIN permission AS C ON A.permission_id=C.id
    @Select(
            "SELECT B.name AS roleName,C.url FROM role_permission AS A " +
            "LEFT JOIN role AS B ON A.role_id=B.id " +
            "LEFT JOIN permission AS C ON A.permission_id=C.id ")
    List<TbRolePermisson> getRolePermissons();
}
